
var jwt = require('jsonwebtoken');

module.exports = {
    create(tokeninfo) {
        return jwt.sign({
            data: tokeninfo,
            exp: (Date.now()/1000)+ (60*60) //1小时有效期
        }, "ruyulu2025");
    },
    verify(token) {
        try {
            var decode = jwt.verify(token, "ruyulu2025")
            return decode
        }
        catch (err) {
            return null
        }
    },
    /**
     * JWT中间件
     * */
    middle() {
        return (req, res, next) =>{
            const token = req.header("x-access-token")
            var ok = jwt.verify(token, "ruyulu2025")
            if (ok == null) {
                res.status(400)
                res.end()
                return
            }
            req.user = ok
            next()
        }
    }
}